/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import App.Facada;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Jaso, Space
 */
public class WithdrawPanel extends Panel implements ActionListener{
    private GUI gui;
    private Facada facada;
    
    private Button btn100 , btn200 , btn500 , btn1000 , btnCancel , btnOK;
    private TextField txtCustomAmount;
    private Label lblCurrentBalance, lblTitle;
    private double currentBalance , withdrawAmount;
    private String title;
    private Label lblErrorMsg;
    private int width;

    public WithdrawPanel(Facada facada)   {
        title = "Withdraw Money";
        this.facada = facada;
        width = 700;
        initComponents() ;
    }

    
    public void initComponents() {
        this.setBackground(new Color(143, 188, 143));  
        this.setBounds(0, 0, width, 500);
        this.setLayout(null);
        
        lblTitle = new Label(title);
        lblTitle.setFont(new Font("Serif", Font.PLAIN, 35));
        lblTitle.setBackground(new Color (143, 178, 143));
        lblTitle.setBounds(width/2-125, 40, 250  , 50);
        add(lblTitle);
        
        lblErrorMsg = new Label();
        lblErrorMsg.setFont(new Font("Serif", Font.PLAIN, 25));
        lblErrorMsg.setForeground(new Color(255, 0, 0));
        lblErrorMsg.setBounds(width/2-140, 260, 280  , 50);
        add(lblErrorMsg);
        
        
        lblCurrentBalance = new Label("Current Balance: "+ currentBalance);
        lblCurrentBalance.setFont(new Font("Serif", Font.PLAIN, 30));
        lblCurrentBalance.setForeground (new Color (173, 255, 47));
        lblCurrentBalance.setBounds(width/2-160, 120, 320  , 50);
        add(lblCurrentBalance);
        
        
        btn1000 = createButton ("1 000");
        btn1000.setBounds(10, 200, 100  , 40);
        add(btn1000);
        
        btn200 = createButton ("200");
        btn200.setBounds(width - 120, 200, 100  , 40);
        add(btn200);
        
        
        btn500 = createButton ("500");
        btn500.setBounds(10, 350, 100  , 40);
        add(btn500);
        
        btn100 = createButton ("100");
        btn100.setBounds(width - 120, 350, 100  , 40);
        add(btn100);
        
        
        btnCancel = createButton ("Cancel");
        btnCancel.setBounds(width/2-110, 400, 100  , 40);
        add(btnCancel);
        
        btnOK = createButton ("OK");
        btnOK.setBounds(width/2 + 40, 400, 100  , 40);
        add(btnOK);
        
        txtCustomAmount = new TextField();
        txtCustomAmount.setFont(new Font("Serif", Font.PLAIN, 30));
        txtCustomAmount.setBounds(width/2-75, 310, 150  , 40);
        add(txtCustomAmount);
        
        
    }
    
    public Button createButton(String name)    {
        Button button = new Button(name);
        button.addActionListener(this);
        button.setBackground(new Color(189, 183, 107));
        button.setFont(new Font("Serif", Font.PLAIN, 30));
        return button;
    }
        
        
    public void updateCurrentBalance(double balance)  {
        lblCurrentBalance.setText("Current Balance: "+ balance);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == btnOK)    { 
            withdraw() ;
        } else if(e.getSource() == btnCancel)    { 
            gui.SwitchPanels("account");
        } else if(e.getSource() == btn100)    { 
            txtCustomAmount.setText("100");
        } else if(e.getSource() == btn200)    { 
            txtCustomAmount.setText("200");
        } else if(e.getSource() == btn500)    { 
            txtCustomAmount.setText("500");
        } else if(e.getSource() == btn1000)    { 
            txtCustomAmount.setText("1000");
        }
    }

    public void withdraw()  {
        if(isValidInput() &&  facada.isValidWithdrawAmount() )    {
            if(yesOrNoOption(getWithdrawAmount()) )    {
                facada.withDrawMoney();
            }
        }
        lblErrorMsg.setText("Please enter valid amount ");
        double start = System.currentTimeMillis();
        
        while(System.currentTimeMillis() - start < 1500){
            lblErrorMsg.setVisible(true);
        }
        lblErrorMsg.setVisible(false);
    }
    
    public void clearTextFields()   {
        txtCustomAmount.setText(null);
    }
    
    
    
    public boolean isValidInput()   {
        try {
            setWithdrawAmount(Double.parseDouble(txtCustomAmount.getText()));
            return true;
        } catch (Exception e) {
            lblErrorMsg.setText("Please enter valid amount ");
            double start = System.currentTimeMillis();
        
            while(System.currentTimeMillis() - start < 1500){
                lblErrorMsg.setVisible(true);
            }
            lblErrorMsg.setVisible(false);
            return false;
        }

    }
    
    
    /**
     * @param gui the gui to set
     */
    public void setGui(GUI gui) {
        this.gui = gui;
    }

    /**
     * @return the CurrentBalance
     */
    public double getCurrentBalance() {
        return currentBalance;
    }

    /**
     * @param CurrentBalance the CurrentBalance to set
     */
    public void setCurrentBalance(double CurrentBalance) {
        this.currentBalance = CurrentBalance;
    }

    public boolean yesOrNoOption(double amount)  {
        String message = "Do you really want withdraw " + getWithdrawAmount() ;
        String title = "Confirmation";
        return JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
    }

    /**
     * @return the withdrawAmount
     */
    public double getWithdrawAmount() {
        return withdrawAmount;
    }

    /**
     * @param withdrawAmount the withdrawAmount to set
     */
    public void setWithdrawAmount(double withdrawAmount) {
        this.withdrawAmount = withdrawAmount;
    }
    
}
